package com.zch.user.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

/**
 * 字符编码过滤器
 * @author Administrator
 *
 */
@WebFilter("/*")//任何地址都会自动执行该过滤器
public class CharSet implements Filter{

	@Override
	public void destroy() {
		System.out.println("过滤器的销毁方法被调用");
	}
	@Override
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
			throws IOException, ServletException {//chain是过滤器链（多个过滤器）
		System.out.println("CharSet的doFilter方法被执行了");
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		//让页面继续往下执行
		chain.doFilter(req, resp);//过滤器链	
	}
	@Override
	public void init(FilterConfig arg0) throws ServletException {
		System.out.println("CharSet的初始化被调用");	
	}	
}
